<template>
  <div class="meterCheck">
    <div class="counts" align="center">
      <div class="btn-group btn-group-justified">
        <div class="btn-group">
          <button class="btn btn-primary" @click="getAll">
            所有表数
            <br />
            {{counts}}
          </button>
        </div>
        <div class="btn-group">
          <button class="btn btn-success" @click="getSuccess">
            成功表数
            <br />
            {{successCounts}}
          </button>
        </div>
        <div class="btn-group">
          <button class="btn btn-warning" @click="getFailed">
            失败表数
            <br />
            {{failedCounts}}
          </button>
        </div>
        <div class="btn-group">
          <button class="btn btn-info" @click="updateData(null, 1, 0)">
            刷新所有
            <br />水表数据
          </button>
        </div>
        <div class="btn-group">
          <button class="btn btn-danger" @click="deleteData(null, 0)">
            删除所有
            <br />水表数据
          </button>
        </div>
      </div>
    </div>
    <!-- <button type="button" class="btn btn-primary" @click="getAll">
        <p>所有表数</p>
        <p>{{counts}}</p>
      </button>
      <button type="button" class="btn btn-success" @click="getSuccess">
        <p>成功表数</p>
        <p>{{successCounts}}</p>
      </button>
      <button type="button" class="btn btn-warning" @click="getFailed">
        <p>失败表数</p>
        <p>{{failedCounts}}</p>
      </button>
      <button type="button" class="btn btn-info" @click="updateData(null, 1, 0)">
        刷新所有数据
      </button>
      <button type="button" class="btn btn-danger" @click="deleteData(null, 0)">
        删除所有数据
    </button>-->
    <div class="query" align="center">
      <el-input
        type="text"
        class="input"
        v-model="deviceNo"
        placeholder="请输入表编号"
        @keyup.enter.native="getNewDeviceByDeviceNo(deviceNo)"
      >
        <template slot="prepend">查询</template>
        <el-button slot="append" icon="el-icon-search" @click="getNewDeviceByDeviceNo(deviceNo)"></el-button>
      </el-input>
      <!-- <el-button type="primary" icon="el-icon-search" @click="getNewDeviceByDeviceNo(deviceNo)" plain></el-button> -->
    </div>

    <div class="title">
      <i class="el-icon-arrow-right">&nbsp;{{title}}</i>
    </div>
    <el-table
      v-loading="meterCheckLoading"
      :data="handleMeterCheckList"
      stripe
      header-cell-class-name="tableRow"
      cell-class-name="tableRow"
      highlight-current-row
    >
      <!-- <el-table-column type="selection" fixed></el-table-column> -->
      <el-table-column type="index" label="序号" fixed></el-table-column>
      <el-table-column prop="deviceNo" label="表编号" min-width="120" fixed></el-table-column>
      <el-table-column prop="macAddr" label="表地址" min-width="120"></el-table-column>
      <el-table-column prop="readValue" label="表读数" min-width="65"></el-table-column>
      <el-table-column prop="readTime" label="抄表时间" min-width="140"></el-table-column>
      <el-table-column label="阀门状态" min-width="75">
        <template slot-scope="scope">
          <el-tooltip placement="right" effect="light">
            <div slot="content">点击可刷新阀门状态</div>
            <el-tag
              type="danger"
              v-if="scope.row.valve === 0"
              @click="updateData(scope.row, 1, 2)"
            >关</el-tag>
          </el-tooltip>
          <el-tooltip placement="right" effect="light">
            <div slot="content">点击可刷新阀门状态</div>
            <el-tag
              type="warning"
              v-if="scope.row.valve === 1"
              @click="updateData(scope.row, 1, 2)"
            >无阀/不定</el-tag>
          </el-tooltip>
          <el-tooltip placement="right" effect="light">
            <div slot="content">点击可刷新阀门状态</div>
            <el-tag
              type="success"
              v-if="scope.row.valve === 2"
              @click="updateData(scope.row, 1, 2)"
            >开</el-tag>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="nbDeviceType" label="表类型" min-width="65"></el-table-column>
      <!-- <el-table-column prop="pinStatus" label="干簧管状态" min-width="100"></el-table-column> -->
      <el-table-column prop="rssi" label="信号强度" min-width="75"></el-table-column>
      <el-table-column prop="batteryVoltage" label="电量(mV)" min-width="80"></el-table-column>
      <el-table-column prop="imei" label="IMEI" min-width="120"></el-table-column>
      <el-table-column label="操作" min-width="80">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button type="primary" plain>
              <i class="el-icon-more"></i><i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button
                  type="text"
                  style="font-size: 1.6em; color: #67c23a"
                  :disabled="scope.row.valve === 2 || scope.row.valve === 1"
                  icon="el-icon-switch-button"
                  @click="controlValve(scope.row, 2, 2)"
                >开阀
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  type="text"
                  style="font-size: 1.6em; color: #f56c6c"
                  :disabled="scope.row.valve === 0 || scope.row.valve === 1"
                  icon="el-icon-switch-button"
                  @click="controlValve(scope.row, 2, 0)"
                >关阀
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  type="text"
                  style="font-size: 1.6em; color: #ec971f"
                  icon="el-icon-check"
                  @click="getData(scope.row,0)"
                >获取</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  type="text"
                  style="font-size: 1.6em"
                  icon="el-icon-refresh"
                  @click="updateData(scope.row, 1, 1)"
                >刷新</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  type="text"
                  style="font-size: 1.6em; color: red"
                  icon="el-icon-delete"
                  @click="deleteData(scope.row, 1)"
                >删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <!-- <el-table-column label="开阀" min-width="50" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            style="font-size: 1.6em; color: #67c23a"
            :disabled="scope.row.valve === 2 || scope.row.valve === 1"
            @click="controlValve(scope.row, 2, 2)"
          >
            <i class="fa fa-power-off"></i>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="关阀" min-width="50" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            style="font-size: 1.6em; color: #f56c6c"
            :disabled="scope.row.valve === 0 || scope.row.valve === 1"
            @click="controlValve(scope.row, 2, 0)"
          >
            <i class="fa fa-power-off"></i>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="获取数据" min-width="75" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            style="font-size: 1.6em; color: #ec971f"
            icon="el-icon-check"
            @click="getData(scope.row,0)"
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column label="刷新数据" min-width="75" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            style="font-size: 1.6em"
            icon="el-icon-refresh"
            @click="updateData(scope.row, 1, 1)"
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column label="删除" min-width="55" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            style="font-size: 1.6em; color: red"
            icon="el-icon-delete"
            @click="deleteData(scope.row, 1)"
          ></el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <!-- 分页 -->
    <el-pagination
      style="margin-top: 20px; text-align: center"
      @size-change="sChange"
      @current-change="pChange"
      :current-page="page"
      :page-sizes="[10, 15, 20, 25]"
      :page-size="rows"
      background
      layout=" total, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import {
  getMeterCheckList,
  getDeviceDetailInfo,
  controlNewDevice,
  updateNewDevice,
  deleteNewDevice
} from "../../../api/api";
import moment from "moment";
export default {
  data() {
    return {
      page: 1, // 分页，页码
      rows: 15, // 分页，每页数据条数
      title: "所有表",
      sort: null,
      total: null,
      counts: null,
      successCounts: null,
      failedCounts: null,
      deviceNo: null,
      enprNo: null,
      flag: null,
      meterCheckList: [],
      meterCheckLoading: false,
      refreshWhich: null,
      refreshWhat: null,
      deleteWhich: null,
      onOrOff: null
    };
  },
  computed: {
    handleMeterCheckList: function() {
      if (this.meterCheckList == 0) return;
      this.meterCheckList.forEach(item => {
        switch (item.nbDeviceType) {
          case 7:
            item.nbDeviceType = "NB水表";
            break;
          default:
            item.nbDeviceType = "未知";
            break;
        }
        item.readTime = moment(item.readTime).format("YYYY-MM-DD HH:mm:ss");
      });
      return this.meterCheckList;
    }
  },
  methods: {
    // 获取出厂表
    GetMeterCheckList(sort, page, rows) {
      let that = this;
      that.meterCheckLoading = true;
      this.isPagination = false;
      let msg = {
        enprNo: 'esss',
        page: page,
        rows: rows,
        sort: sort
      };
      getMeterCheckList(msg)
        .then(res => {
          if (res.data.code === "200") {
            if (sort === 0) {
              that.sort = 0;
              that.meterCheckList = res.data.data.content;
              that.total = res.data.counts;
              that.title = "所有表";
              that.counts = res.data.counts;
              that.successCounts = res.data.success;
              that.failedCounts = that.counts - that.successCounts;
            } else if (sort === 1) {
              that.sort = 1;
              that.total = res.data.data.totalElements;
              that.title = "成功表";
              that.meterCheckList = res.data.data.content;
            } else {
              that.sort = 2;
              that.total = res.data.data.totalElements;
              that.title = "失败表";
              that.meterCheckList = res.data.data.content;
            }
            that.meterCheckLoading = false;
          } else {
            that.Error("获取数据失败！");
            that.meterCheckLoading = false;
          }
        })
        .catch(error => {
          // that.Error(error);
          that.meterCheckLoading = false;
        });
    },

    /* 获取所有表、成功表、失败表 */
    getAll() {
      let page = this.page;
      let rows = this.rows;
      let sort = 0;
      this.GetMeterCheckList(sort, page, rows);
    },
    getSuccess() {
      let page = this.page;
      let rows = this.rows;
      let sort = 1;
      this.GetMeterCheckList(sort, page, rows);
    },
    getFailed() {
      let page = this.page;
      let rows = this.rows;
      let sort = 2;
      this.GetMeterCheckList(sort, page, rows);
    },

    // 根据表编号查询
    getNewDeviceByDeviceNo(deviceNo) {
      let that = this;
      let msg = {
        deviceNo: that.deviceNo,
        enprNo: 'esss',
        flag: 1
      };
      that.meterCheckList = [];
      getDeviceDetailInfo(msg)
        .then(res => {
          if (res.data.code == 200) {
            that.total = 1;
            that.title = "查询结果";
            that.meterCheckList.push(res.data.data);
          } else {
            that.Error("请输入正确的表编号!");
          }
        })
        .catch(error => {
          // that.Error(error);
        });
    },

    /* 控制 */
    getData(row, commandNo) {
      let that = this;
      let msg = {
        macAddr: row.macAddr,
        command: commandNo,
        value01: 0
      };
      that
        .$confirm("此操作将发送获取数据指令，确定吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "info"
        })
        .then(() => {
          controlNewDevice(msg)
            .then(res => {
              if (res.data.code == 200) {
                that.Success("获取数据指令发送成功！");
                return;
              } else {
                that.Error("获取数据指令失败！");
              }
            })
            .catch(err => {
              // taht.Error(err);
            });
        });
    },

    /* // 初始化数据
    initiate(row, commandNo) {
      let that = this;
      let msg = {
        macAddr: row.macAddr,
        command: commandNo,
        value01: 0
      };
      that
        .$confirm("此操作将初始化当前水表，确定吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "info"
        })
        .then(() => {
          controlNewDevice(msg)
            .then(res => {
              if (res.data.code == 200) {
                that.Success("初始化成功！");
              } else {
                that.Error("初始化失败！");
              }
            })
            .catch(err => {
              taht.Error(err);
            });
        });
    }, */

    // 开关阀
    controlValve(row, commandNo, onOff) {
      let that = this;
      let msg = {
        macAddr: row.macAddr,
        command: commandNo,
        value01: onOff
      };
      that.onOrOff = onOff ? "开阀" : "关阀";
      that
        .$confirm("此操作将发送" + that.onOrOff + "指令,确定吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "info"
        })
        .then(() => {
          controlNewDevice(msg)
            .then(res => {
              if (res.data.code == 200) {
                that.Success(that.onOrOff + "指令发送成功！");
              } else {
                that.Error(that.onOrOff + "指令发送失败！");
              }
            })
            .catch(() => {
              // taht.Error(err);
            });
        });
    },

    // 刷新数据
    updateData(row, check, flag) {
      let that = this;
      let msg = {
        check: check,
        flag: flag,
        macAddr: row ? row.macAddr : row
      };
      that.refreshWhich = flag === 0 ? "所有" : "当前";
      that.refreshWhat = flag === 2 ? "阀门状态" : "全部数据";
      that
        .$confirm(
          "此操作将刷新" +
            that.refreshWhich +
            "水表" +
            that.refreshWhat +
            "，确定吗？",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "info"
          }
        )
        .then(() => {
          updateNewDevice(msg)
            .then(res => {
              if (res.data.code == 200) {
                that.Success("刷新数据指令发送成功！");
              } else {
                that.Error("刷新数据指令发送失败！");
              }
            })
            .catch(err => {
              // that.Error(err);
            });
        });
    },

    // 删除数据
    deleteData(row, flag) {
      let that = this;
      let msg = {
        enprNo: 'esss',
        flag: flag,
        imei: row ? row.imei : row
      };
      that.deleteWhich = flag === 0 ? "所有" : "当前";
      that
        .$confirm(
          "此操作将删除" + that.deleteWhich + "水表，确定吗？",
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }
        )
        .then(() => {
          deleteNewDevice(msg)
            .then(res => {
              if (res.data.code == 200) {
                that.getAll();
                that.Success("删除成功！");
              } else {
                that.Error("删除失败！");
              }
            })
            .catch(error => {
              // that.Error(error);
            });
        });
    },

    // 分页
    pChange(val) {
      let rows = this.rows;
      if (this.sort === 0) {
        this.GetMeterCheckList(0, val, rows);
      } else if (this.sort === 1) {
        this.GetMeterCheckList(1, val, rows);
      } else {
        this.GetMeterCheckList(2, val, rows);
      }
    },
    sChange(val) {
      let page = this.page;
      if (this.sort === 0) {
        this.GetMeterCheckList(0, page, val);
      } else if (this.sort === 1) {
        this.GetMeterCheckList(1, page, val);
      } else {
        this.GetMeterCheckList(2, page, val);
      }
    },

    // 提示框
    Success(str) {
      this.$alert(String(str), "提示", {
        confirmButtonText: "确定",
        type: "success"
      });
    },
    Warning(str) {
      this.$alert(String(str), "提示", {
        confirmButtonText: "确定",
        type: "warning"
      });
    },
    Error(str) {
      this.$alert(String(str), "提示", {
        confirmButtonText: "确定",
        type: "error"
      });
    },
    Info(str) {
      this.$alert(String(str), "提示", {
        confirmButtonText: "确定",
        type: "info"
      });
    }
  },

  mounted() {
    this.getAll();
  }
};
</script>

<style scoped>
/* 输入框样式 */
.input {
  min-width: 30%;
  max-width: 50%;
  margin: 10px auto;
}
</style>